﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StoreClientModule;
using uCoreClasses;
using UploadManager;

namespace MBBStoreClient
{
    public partial class frmUpdate : Form
    {
        SynchronizeData sync = new SynchronizeData();
        uThreader t = new uThreader();

        public frmUpdate()
        {
            InitializeComponent();

            btnClose.Click += new EventHandler(btnClose_Click);
            panel2.EnabledChanged += new EventHandler(panel2_EnabledChanged);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);

            sync.ProcessCompleted += new UnityCore.CoreHandler(sync_ProcessCompleted);
            sync.ProcessError += new UnityCore.CoreHandler(sync_ProcessError);
            sync.ProcessProgress += new UnityCore.CoreHandler(sync_ProcessProgress);
        }

        void sync_ProcessProgress(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                lstUpdate.Items.Add(msg);
                lstUpdate.SelectedIndex = lstUpdate.Items.Count - 1;
            });
        }

        void sync_ProcessError(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                lstUpdate.Items.Add(msg);
                lstUpdate.SelectedIndex = lstUpdate.Items.Count - 1;
                MessageBox.Show(msg);
                prgBar.MarqueeAnimationSpeed = 0;
                prgBar.Refresh();
                btnClose.Enabled = true;
               
            });
        }

        void sync_ProcessCompleted(string pcode, string msg)
        {
            t.SafeCall(this, delegate
            {
                switch (pcode)
                {
                    case "Download":
                        prgBar.MarqueeAnimationSpeed = 0;
                        btnClose.Enabled = true;
                        prgBar.Refresh();
                        MessageBox.Show("Update Completed");
                        break;
                }

            });
        }
      
        void btnUpdate_Click(object sender, EventArgs e)
        {
            btnUpdate.Enabled = false;
            btnClose.Enabled = false;
            prgBar.MarqueeAnimationSpeed = 10;
            sync.Download();
        }   
        public void AddData(string str)
        {
            lstUpdate.Items.Add(str);
            lstUpdate.SelectedIndex = lstUpdate.Items.Count - 1;
        }


        void panel2_EnabledChanged(object sender, EventArgs e)
        {
            if (!panel2.Enabled)
            {
                panel2.BackColor = Color.MidnightBlue;
            }
            else
            {
                panel2.BackColor = Color.RoyalBlue;
            }
        }
        void btnClose_Click(object sender, EventArgs e)
        {
            pnlForm.Dispose();
            this.Dispose();
        }
        public Panel PanelForm
        {
            get { return pnlForm; }
        }
        public void Initialize(string Title)
        {
            this.Select();
            lblText.Text = Title;
            prgBar.MarqueeAnimationSpeed = 0;
            
        }
    }
}
